'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const now = new Date();
    
    // 先删除已存在的配置（强制替换）
    await queryInterface.bulkDelete('table_configs', {
      code: 'menus'
    }, {});
    
    await queryInterface.bulkInsert('table_configs', [{
      id: Sequelize.literal('UUID()'),
      code: 'menus',
      name: '菜单管理',
      description: '系统菜单导航管理',
      table_name: 'menus',
      api_path: '/menus',

      fields: JSON.stringify([
        {
          key: 'id',
          label: 'ID',
          type: 'text',
          table: { hidden: true },
          search: { searchable: false },
          form: { creatable: false, editable: false }
        },
        {
          key: 'title',
          label: '菜单标题',
          type: 'text',
          table: { width: 200, align: 'left', sortable: true },
          search: { searchable: true, searchType: 'like', quickSearch: true },
          form: {
            creatable: true,
            editable: true,
            required: true,
            rules: [{ required: true, message: '请输入菜单标题' }],
            placeholder: '请输入菜单标题'
          }
        },
        {
          key: 'path',
          label: '路由路径',
          type: 'text',
          table: { width: 200, align: 'left' },
          search: { searchable: true, searchType: 'like' },
          form: {
            creatable: true,
            editable: true,
            placeholder: '请输入路由路径'
          }
        },
        {
          key: 'icon',
          label: '菜单图标',
          type: 'text',
          table: { width: 120, align: 'center' },
          search: { searchable: false },
          form: {
            creatable: true,
            editable: true,
            placeholder: '请输入图标名称'
          }
        },
        {
          key: 'parentId',
          label: '上级菜单',
          type: 'select',
          table: {
            width: 150,
            align: 'left',
            render: 'relation',
            relationField: 'parent.title'
          },
          search: { searchable: false },
          form: {
            creatable: true,
            editable: true,
            placeholder: '请选择上级菜单（可选）',
            optionsSource: 'api',
            optionsApi: '/menus',
            optionsLabelField: 'title',
            optionsValueField: 'id'
          }
        },
        {
          key: 'sort',
          label: '排序',
          type: 'number',
          table: { width: 100, align: 'center', sortable: true },
          search: { searchable: false },
          form: {
            creatable: true,
            editable: true,
            placeholder: '请输入排序号',
            componentProps: { min: 0 }
          }
        },
        {
          key: 'hidden',
          label: '是否隐藏',
          type: 'select',
          table: {
            width: 100,
            align: 'center',
            render: 'tag',
            tagColorMap: { true: 'default', false: 'success' },
            tagLabelMap: { true: '隐藏', false: '显示' }
          },
          search: { searchable: false },
          form: {
            creatable: true,
            editable: true,
            options: [
              { label: '显示', value: false },
              { label: '隐藏', value: true }
            ]
          }
        },
        {
          key: 'createdAt',
          label: '创建时间',
          type: 'datetime',
          table: { width: 180, sortable: true, format: 'YYYY-MM-DD HH:mm:ss' },
          search: { searchable: false },
          form: { creatable: false, editable: false }
        }
      ]),

      actions: JSON.stringify({
        create: true,
        update: true,
        delete: true,
        batchDelete: true,
        export: true
      }),

      table_props: JSON.stringify({
        size: 'middle',
        rowKey: 'id',
        pagination: { pageSize: 10, showSizeChanger: true },
        rowSelection: true
      }),

      permissions: JSON.stringify({
        view: 'menu:read',
        create: 'menu:create',
        update: 'menu:update',
        delete: 'menu:delete'
      }),

      status: true,
      created_at: now,
      updated_at: now
    }], {});
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('table_configs', { code: 'menus' }, {});
  }
};

